package user

import (
	"github.com/gin-gonic/gin"
	"mz_water/entity"
	models "mz_water/model"
	"net/http"
	"strings"
)

func GetUserList(c *gin.Context) {
	id := c.Query("id")
	res := models.GetUser(id)
	c.JSON(http.StatusOK, res)
}
func GetAllList(c *gin.Context) {
	res := models.GetUserList(c)
	c.JSON(http.StatusOK, res)
}

/*
* @param
* 添加微信小程序用户
 */
func AddUser(c *gin.Context) {
	user := &models.Wx_user{}
	res := entity.Result{}
	token := c.Request.Header.Get("token")
	openid := strings.Split(token, "|")
	user.NickName = c.PostForm("nickName")
	user.AvatarUrl = c.PostForm("avatarUrl")
	user.City = c.PostForm("city")
	user.Country = c.PostForm("country")
	user.Province = c.PostForm("province")
	user.Gender = c.PostForm("gender")
	user.Openid = openid[0]
	_, err := models.CreateUser(*user)
	if err != nil {
		res.SetCode(201)
		res.SetData(err)
		res.SetMessage("记录用户失败")
		c.JSON(http.StatusOK, res)
		return
	}
	res.SetCode(200)
	res.SetData("")
	res.SetMessage("记录用户成功")
	c.JSON(http.StatusOK, res)
}
